﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;
using YQGL.DBUtility;//Please add references
namespace YQGL.DAL
{
    /// <summary>
    /// 数据访问类:SB_QYBB
    /// </summary>
    public partial class SB_QYBB
    {
        public SB_QYBB()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SB_QYBB");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YQGL.Model.SB_QYBB model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SB_QYBB(");
            strSql.Append("QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,SSYF,SSND)");
            strSql.Append(" values (");
            strSql.Append("@QYBM,@BBXM_DM,@JE,@SSSQ_Q,@SSSQ_Z,@Lrrq,@ZTDM,@SHR_DM,@SHRQ,@LC,@SSYF,@SSND)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@QYBM", SqlDbType.VarChar,20),
					new SqlParameter("@BBXM_DM", SqlDbType.VarChar,2),
					new SqlParameter("@JE", SqlDbType.Decimal,9),
					new SqlParameter("@SSSQ_Q", SqlDbType.DateTime),
					new SqlParameter("@SSSQ_Z", SqlDbType.DateTime),
					new SqlParameter("@Lrrq", SqlDbType.DateTime),
					new SqlParameter("@ZTDM", SqlDbType.Char,1),
					new SqlParameter("@SHR_DM", SqlDbType.Char,8),
					new SqlParameter("@SHRQ", SqlDbType.DateTime),
					new SqlParameter("@LC", SqlDbType.Char,1),
					new SqlParameter("@SSYF", SqlDbType.Char,6),
					new SqlParameter("@SSND", SqlDbType.Char,4)};
            parameters[0].Value = model.QYBM;
            parameters[1].Value = model.BBXM_DM;
            parameters[2].Value = model.JE;
            parameters[3].Value = model.SSSQ_Q;
            parameters[4].Value = model.SSSQ_Z;
            parameters[5].Value = model.Lrrq;
            parameters[6].Value = model.ZTDM;
            parameters[7].Value = model.SHR_DM;
            parameters[8].Value = model.SHRQ;
            parameters[9].Value = model.LC;
            parameters[10].Value = model.SSYF;
            parameters[11].Value = model.SSND;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(YQGL.Model.SB_QYBB model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SB_QYBB set ");
            strSql.Append("JE=@JE,");
            strSql.Append("Lrrq=@Lrrq,");
            strSql.Append("ZTDM=@ZTDM,");
            strSql.Append("SHR_DM=@SHR_DM,");
            strSql.Append("SHRQ=@SHRQ,");
            strSql.Append("LC=@LC,");
            strSql.Append("SSYF=@SSYF,");
            strSql.Append("SSND=@SSND");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@JE", SqlDbType.Decimal,9),
					new SqlParameter("@Lrrq", SqlDbType.DateTime),
					new SqlParameter("@ZTDM", SqlDbType.Char,1),
					new SqlParameter("@SHR_DM", SqlDbType.Char,8),
					new SqlParameter("@SHRQ", SqlDbType.DateTime),
					new SqlParameter("@LC", SqlDbType.Char,1),
					new SqlParameter("@SSYF", SqlDbType.Char,6),
					new SqlParameter("@SSND", SqlDbType.Char,4),
					new SqlParameter("@QYBM", SqlDbType.VarChar,20),
					new SqlParameter("@BBXM_DM", SqlDbType.VarChar,2),
					new SqlParameter("@SSSQ_Q", SqlDbType.DateTime),
					new SqlParameter("@SSSQ_Z", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.JE;
            parameters[1].Value = model.Lrrq;
            parameters[2].Value = model.ZTDM;
            parameters[3].Value = model.SHR_DM;
            parameters[4].Value = model.SHRQ;
            parameters[5].Value = model.LC;
            parameters[6].Value = model.SSYF;
            parameters[7].Value = model.SSND;
            parameters[8].Value = model.QYBM;
            parameters[9].Value = model.BBXM_DM;
            parameters[10].Value = model.SSSQ_Q;
            parameters[11].Value = model.SSSQ_Z;
            parameters[12].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SB_QYBB ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SB_QYBB ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YQGL.Model.SB_QYBB GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,ID,SSYF,SSND,SHXX from SB_QYBB ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            YQGL.Model.SB_QYBB model = new YQGL.Model.SB_QYBB();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YQGL.Model.SB_QYBB DataRowToModel(DataRow row)
        {
            YQGL.Model.SB_QYBB model = new YQGL.Model.SB_QYBB();
            if (row != null)
            {
                if (row["QYBM"] != null)
                {
                    model.QYBM = row["QYBM"].ToString();
                }
                if (row["BBXM_DM"] != null)
                {
                    model.BBXM_DM = row["BBXM_DM"].ToString();
                }
                if (row["JE"] != null && row["JE"].ToString() != "")
                {
                    model.JE = decimal.Parse(row["JE"].ToString());
                }
                if (row["SSSQ_Q"] != null && row["SSSQ_Q"].ToString() != "")
                {
                    model.SSSQ_Q = DateTime.Parse(row["SSSQ_Q"].ToString());
                }
                if (row["SSSQ_Z"] != null && row["SSSQ_Z"].ToString() != "")
                {
                    model.SSSQ_Z = DateTime.Parse(row["SSSQ_Z"].ToString());
                }
                if (row["Lrrq"] != null && row["Lrrq"].ToString() != "")
                {
                    model.Lrrq = DateTime.Parse(row["Lrrq"].ToString());
                }
                if (row["ZTDM"] != null)
                {
                    model.ZTDM = row["ZTDM"].ToString();
                }
                if (row["SHR_DM"] != null)
                {
                    model.SHR_DM = row["SHR_DM"].ToString();
                }
                if (row["SHRQ"] != null && row["SHRQ"].ToString() != "")
                {
                    model.SHRQ = DateTime.Parse(row["SHRQ"].ToString());
                }
                if (row["LC"] != null)
                {
                    model.LC = row["LC"].ToString();
                }
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["SSYF"] != null)
                {
                    model.SSYF = row["SSYF"].ToString();
                }
                if (row["SSND"] != null)
                {
                    model.SSND = row["SSND"].ToString();
                }
                if (row["SHXX"] != null)
                {
                    model.SHXX = row["SHXX"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,ID,SSYF,SSND,SHXX ");
            strSql.Append(" FROM SB_QYBB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,ID,SSYF,SSND,SHXX ");
            strSql.Append(" FROM SB_QYBB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SB_QYBB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from SB_QYBB T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "SB_QYBB";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        public void AddList(List<YQGL.Model.SB_QYBB> modelList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();

            foreach (YQGL.Model.SB_QYBB model in modelList)
            {


                strSql = new StringBuilder();
                strSql.Append("insert into SB_QYBB(");
                strSql.Append("QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,SSYF,SSND)");
                strSql.Append(" values (");
                strSql.Append("@QYBM,@BBXM_DM,@JE,@SSSQ_Q,@SSSQ_Z,@Lrrq,@ZTDM,@SHR_DM,@SHRQ,@LC,@SSYF,@SSND)");

                SqlParameter[] parameters = {
					new SqlParameter("@QYBM", SqlDbType.VarChar,20),
					new SqlParameter("@BBXM_DM", SqlDbType.VarChar,2),
					new SqlParameter("@JE", SqlDbType.Decimal,9),
					new SqlParameter("@SSSQ_Q", SqlDbType.DateTime),
					new SqlParameter("@SSSQ_Z", SqlDbType.DateTime),
					new SqlParameter("@Lrrq", SqlDbType.DateTime),
					new SqlParameter("@ZTDM", SqlDbType.Char,1),
					new SqlParameter("@SHR_DM", SqlDbType.Char,8),
					new SqlParameter("@SHRQ", SqlDbType.DateTime),
					new SqlParameter("@LC", SqlDbType.Char,1),
					new SqlParameter("@SSYF", SqlDbType.Char,6),
					new SqlParameter("@SSND", SqlDbType.Char,4)};
                parameters[0].Value = model.QYBM;
                parameters[1].Value = model.BBXM_DM;
                parameters[2].Value = model.JE;
                parameters[3].Value = model.SSSQ_Q;
                parameters[4].Value = model.SSSQ_Z;
                parameters[5].Value = model.Lrrq;
                parameters[6].Value = model.ZTDM;
                parameters[7].Value = model.SHR_DM;
                parameters[8].Value = model.SHRQ;
                parameters[9].Value = model.LC;
                parameters[10].Value = model.SSYF;
                parameters[11].Value = model.SSND;
                cmdList.Add(new CommandInfo(strSql.ToString(), parameters));



            }
            
            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);


            }

        }


        public void EditList(List<YQGL.Model.SB_QYBB> modelList, List<YQGL.Model.SB_QYBB> modelListUpadte)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();

            foreach (YQGL.Model.SB_QYBB model in modelList)
            {


                strSql = new StringBuilder();
                strSql.Append("insert into SB_QYBB(");
                strSql.Append("QYBM,BBXM_DM,JE,SSSQ_Q,SSSQ_Z,Lrrq,ZTDM,SHR_DM,SHRQ,LC,SSYF,SSND)");
                strSql.Append(" values (");
                strSql.Append("@QYBM,@BBXM_DM,@JE,@SSSQ_Q,@SSSQ_Z,@Lrrq,@ZTDM,@SHR_DM,@SHRQ,@LC,@SSYF,@SSND)");

                SqlParameter[] parameters = {
					new SqlParameter("@QYBM", SqlDbType.VarChar,20),
					new SqlParameter("@BBXM_DM", SqlDbType.VarChar,2),
					new SqlParameter("@JE", SqlDbType.Decimal,9),
					new SqlParameter("@SSSQ_Q", SqlDbType.DateTime),
					new SqlParameter("@SSSQ_Z", SqlDbType.DateTime),
					new SqlParameter("@Lrrq", SqlDbType.DateTime),
					new SqlParameter("@ZTDM", SqlDbType.Char,1),
					new SqlParameter("@SHR_DM", SqlDbType.Char,8),
					new SqlParameter("@SHRQ", SqlDbType.DateTime),
					new SqlParameter("@LC", SqlDbType.Char,1),
					new SqlParameter("@SSYF", SqlDbType.Char,6),
					new SqlParameter("@SSND", SqlDbType.Char,4)};
                parameters[0].Value = model.QYBM;
                parameters[1].Value = model.BBXM_DM;
                parameters[2].Value = model.JE;
                parameters[3].Value = model.SSSQ_Q;
                parameters[4].Value = model.SSSQ_Z;
                parameters[5].Value = model.Lrrq;
                parameters[6].Value = model.ZTDM;
                parameters[7].Value = model.SHR_DM;
                parameters[8].Value = model.SHRQ;
                parameters[9].Value = model.LC;
                parameters[10].Value = model.SSYF;
                parameters[11].Value = model.SSND;
                cmdList.Add(new CommandInfo(strSql.ToString(), parameters));

            }
            foreach (YQGL.Model.SB_QYBB model in modelListUpadte)
            {
                strSql = new StringBuilder();
                strSql.Append("update SB_QYBB set ");
                strSql.Append("JE=@JE,");
                strSql.Append("Lrrq=@Lrrq ");
               
                strSql.Append(" where ID=@ID");
                SqlParameter[] parameters = {
					new SqlParameter("@JE", SqlDbType.Decimal,9),
					new SqlParameter("@Lrrq", SqlDbType.DateTime),
				
					new SqlParameter("@ID", SqlDbType.Int,4)};
                parameters[0].Value = model.JE;
                parameters[1].Value = model.Lrrq;
               
                parameters[2].Value = model.ID;
                cmdList.Add(new CommandInfo(strSql.ToString(), parameters));
            }
            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);


            }

        }
        #endregion  ExtensionMethod
    }
}

